from os import path
import uuid
from qiniu import Auth, put_file, etag

access_key = '8FKl2sXYdm-gLnAjZCcutLSgyrLu4E0_VcJespbc'
secret_key = 'miCPmQkIypmjZ7O4_HIXJyD_s7oNRtZqWWwLKMhC'
q = Auth(access_key, secret_key)

bucket_name = 'teachassist'


async def upload(filepath):
    if not path.exists(filepath):
        raise FileNotFoundError

    if not path.isfile(filepath):
        raise FileNotFoundError

    key = str(uuid.uuid1())+path.splitext(path.basename(filepath))[1]
    token = q.upload_token(bucket_name, key)
    ret, info = put_file(token, key, filepath, version='v2')
    base_url = f'http://r45g3ztv2.hd-bkt.clouddn.com/{key}'
    return base_url
